<template>
  <basic-container>
    <avue-crud :option="option"
               :data="data"
               :page="page"
               @row-del="rowDel"
               v-model="form"
               :permission="permissionList"
               @row-update="rowUpdate"
               @row-save="rowSave"
               :before-open="beforeOpen"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.appuser_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot="menu"
                slot-scope="scope">
        <el-button v-if="(permission.appuser_add)&&(scope.row.status==1)"
                   size="small"
                   type="text"
                   icon="el-icon-edit"
                   @click="ban(scope.row,scope.index)">禁用
        </el-button>
        <el-button v-if="(permission.appuser_add)&&(scope.row.status!=1)"
                   size="small"
                   type="text"
                   icon="el-icon-edit"
                   @click="unban(scope.row,scope.index)">启用
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove,banUpdate,unBanUpdate} from "@/api/app/appuser";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          tip: false,
          border: true,
          index: true,
          selection: true,
          viewBtn: true,
          dialogHeight: 450,
          column: [
            {
              label: "昵称",
              prop: "nickName",
              span:24,
              search:true,
              rules: [{
                required: true,
                message: "请输入昵称",
                trigger: "blur"
              }]
            },
            {
              label: "账号",
              prop: "account",
              search:true,
              span:24,
              rules: [{required: true,  trigger: 'blur'}]
            },
            {
              label: "密码",
              prop: "password",
              type:"password",
              hide:true,
              rules: [{
                required: true,
                message: "请输入密码",
                trigger: "blur"
              }]
            },
            {
              label: "性别",
              prop: "sex",
              type: "select",
              dicData: [
                {
                  label: "男",
                  value: 1
                },
                {
                  label: "女",
                  value: 2
                }
              ],
              rules: [{
                required: true,
                message: "请输入性别",
                trigger: "blur"
              }]
            },
            {
              label: "职位",
              prop: "job",
              rules: [{
                required: true,
                message: "请输入职位",
                trigger: "blur"
              }]
            },
            {
              label: "创建时间",
              prop: "createDate",
              addDisplay:false,
              editDisplay:false,
              rules: [{
                required: true,
                message: "请输入创建时间",
                trigger: "blur"
              }]
            },
            {
              label: "状态",
              prop: "status",
              type: "select",
              dicData: [
                {
                  label: "启用",
                  value: 1
                },
                {
                  label: "禁用",
                  value: 2
                }
              ],
              rules: [{
                required: true,
                message: "请输入用户状态",
                trigger: "blur"
              }]
            },

            {
              label: "更新时间",
              prop: "updateDate",
              addDisplay:false,
              editDisplay:false,
              hiden:true,
              rules: [{
                required: true,
                message: "请输入更新时间",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
       ban(row){
        banUpdate(row.id).then(()=>{
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "禁用成功!"
        });
        });
      },
      unban(row){
        unBanUpdate(row.id).then(()=>{
          this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "启用成功!"
        });
        });
      },
      rowSave(row, loading) {
        add(row).then(() => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
      },
      rowUpdate(row, index, loading) {
        update(row).then(() => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      searchReset() {
        this.onLoad(this.page);
      },
      searchChange(params) {
        this.onLoad(this.page, params);
      },
      selectionChange(list) {
        console.log(list)
        this.selectionList = list;
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },

      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      onLoad(page, params = {}) {
        getList(page.currentPage, page.pageSize, params).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
        });
      }
    }
  };
</script>

<style>
</style>
